package lolxxs.chenyanting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lolxxs.chenyanting.model.ResumeInformation;
import lolxxs.chenyanting.mapper.ResumeInformationMapper;
import lolxxs.chenyanting.service.IResumeInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lolxxs.chenyanting.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @since 2021-12-30
 */
@Service
public class ResumeInformationServiceImpl extends ServiceImpl<ResumeInformationMapper, ResumeInformation> implements IResumeInformationService {

    @Autowired
    @SuppressWarnings("all")
    //all to suppress all warnings （抑制所有警告）
    // 由于编译器无法检测到mybtis-plus里面的创建bean代码会出现警告
    private ResumeInformationMapper resumeInformationMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public int add(ResumeInformation resumeInformation) {
        Date date = new Date();
        resumeInformation.setResumeId(idWorker.nextId());
        resumeInformation.setCreateTime(date);
        resumeInformation.setUpdateTime(date);
        resumeInformation.setState(1);
        resumeInformation.setLevel(1);
        return resumeInformationMapper.insert(resumeInformation);
    }


    @Override
    public int update(ResumeInformation resumeInformation) {
        // 前端传入简历ID
        resumeInformation.setUpdateTime(new Date());
        return resumeInformationMapper.updateById(resumeInformation);
    }

    @Override
    public List<ResumeInformation> query(ResumeInformation resumeInformation) {
        List<ResumeInformation> resumeInformations = resumeInformationMapper.selectList(queryWrapper(resumeInformation));
        if (resumeInformations.size() > 0) {
            return resumeInformations;
        }
        return null;
    }

    @Override
    public int delete(ResumeInformation resumeInformation) {
        ResumeInformation resumeInformation1 = new ResumeInformation();
        resumeInformation1.setState(0);
        resumeInformation1.setUpdateTime(new Date());
        //将内部修改的值和外部传入的值进行分离，防止外部额外修改信息
        // 外部的值负责选择更新哪些行，内部创建的负责更新行中哪些值
        return resumeInformationMapper.update(resumeInformation1, queryWrapper(resumeInformation));
    }

    private QueryWrapper<ResumeInformation> queryWrapper(ResumeInformation resumeInformation) {
        QueryWrapper<ResumeInformation> wrapper=new QueryWrapper<>();
        if (resumeInformation.getResumeId() != null) {
            wrapper.eq("resumeId", resumeInformation.getResumeId());
        }
        if (resumeInformation.getUserId() != null) {
            wrapper.eq("userId", resumeInformation.getUserId());
        }
        if (resumeInformation.getUserName() != null) {
            wrapper.eq("userName", resumeInformation.getUserName());
        }
        if (resumeInformation.getSummary() != null) {
            wrapper.like("summary", resumeInformation.getSummary());
        }
        if (resumeInformation.getState() != null) {
            wrapper.eq("state", resumeInformation.getState());
        }
        if (resumeInformation.getLevel() != null) {
            wrapper.ge("level", resumeInformation.getLevel());
        }
        if (resumeInformation.getCreateTime() != null) {
            // 大于左边时间createTime
            wrapper.ge("createTime", resumeInformation.getCreateTime());
        }
        if (resumeInformation.getUpdateTime() != null) {
            // 小于右边时间updateTime
            wrapper.le("createTime", resumeInformation.getUpdateTime());
        }
        return wrapper;
    }
}
